package com.prs.crm.service.cdxh.impl;

import com.prs.crm.dao.cdxh.AlreadyDistributionDao;
import com.prs.crm.domain.cdxh.AlreadyDistribution;
import com.prs.crm.domain.cdxh.AlreadyDistributionDtail;
import com.prs.crm.domain.cdxh.Cdstock;
import com.prs.crm.domain.cdxh.CrmCustomer;
import com.prs.crm.domain.cdxh.DistributionOrder;
import com.prs.crm.domain.cdxh.DistributionOrderDetail;
import com.prs.crm.domain.cdxh.SaleOrderDetail;
import com.prs.crm.domain.cdxh.SerialNumberss;
import com.prs.crm.domain.cdxh.StockPrecautionary;
import com.prs.crm.domain.cdxh.cd_Product;
import com.prs.crm.domain.cdxh.distribution.DistributionReturn;
import com.prs.crm.domain.cdxh.distribution.DistributionReturnDetail;
import com.prs.crm.service.cdxh.AlreadyDistributionService;
import com.prs.crm.service.cdxh.CdstockService;
import com.prs.crm.service.cdxh.CrmCustomerService;
import com.prs.crm.service.cdxh.DistributionOrderService;
import com.prs.crm.service.cdxh.DistributionReturnService;
import com.prs.crm.service.cdxh.SaleOrderService;
import com.prs.crm.service.cdxh.SerialNumberssService;
import com.prs.crm.service.cdxh.StockPrecautionaryService;
import com.prs.crm.service.cdxh.cd_ProductService;
import com.prs.crm.service.impl.BaseServiceImpl;

/**
 * @author LanXiaoGang
 * @E-mail:lan9liang@163.com
 * @qq:7857553326
 * @version 创建时间：2010-12-14 下午01:24:51 简单说明
 */

public class AlreadyDistributionServiceImpl extends
		BaseServiceImpl<AlreadyDistribution, AlreadyDistributionDao> implements
		AlreadyDistributionService {

	private DistributionReturnService distributionReturnService;

	private SerialNumberssService serialNumberssService;

	private DistributionOrder distributionOrder;

	private DistributionOrderService distributionOrderService;

	private AlreadyDistribution alreadyDistribution;

	private AlreadyDistributionService alreadyDistributionService;

	private Cdstock cdstock;

	private CdstockService cdstockService;

	private StockPrecautionaryService stockPrecautionaryService;
	
	private cd_ProductService cdProductService;
	
	private CrmCustomerService crmCustomerService;
	
	private SaleOrderService saleOrderService;

	@Override
	public void setAlreadyDistribution(AlreadyDistribution alreadyDistribution,
			DistributionOrder distributionOrder, Integer[] checks,
			String[] packWay, Integer[] proJudges, String[] disNums,
			String[] disNumss,String[] disTotal) {
		int lengths = checks.length;
		int inLengths = proJudges.length;

		int totalNum = 0;
		double totalMony = 0.0;
		String proCode = "PS03092010G1";
		for (int i = 0; i < lengths; i++) {
			DistributionOrderDetail doObject = this
					.getDistributionOrderService().getDistributionOrderDetail(
							checks[i]);
			//------------------------------
			
			/**
			 * 配送编号
			 */
			
			SerialNumberss snObject = this.getSerialNumberssService()
					.getSerialNumberss("配送编号");

			if (snObject != null) {
				proCode = snObject.getStyNumberCode();
				String prosCode[] = (snObject.getStyNumberCode()).split("G");
				int overNums = Integer.parseInt(prosCode[1]) + 1;
				String putValues = prosCode[0] + "G" + overNums;
				snObject.setStyNumberCode(putValues);
				this.getSerialNumberssService().setNumCode(snObject);
			} else {
				SerialNumberss snsObject = new SerialNumberss();
				snsObject.setStyCategory("配送编号");
				snsObject.setStyNumberCode("PS03092010G2");
				this.getSerialNumberssService().save(snsObject);
			}
			
			
			//------
			AlreadyDistributionDtail adObject = new AlreadyDistributionDtail();
			adObject.setProCode(doObject.getProductCode());
			adObject.setProName(doObject.getProductName());
			adObject.setProColor(doObject.getProductColor());
			adObject.setProNorm(doObject.getProductNorm());
			String pack = "";
			int nums = 0;
			int numss = 0;

			for (int j = 0; j < inLengths; j++) {

				if (proJudges[j].equals(checks[i]) || proJudges[j]==checks[i]) {

					pack = packWay[j];
					nums = Integer.parseInt(disNums[j]);
					numss = Integer.parseInt(disNumss[j]);
				}
			}
			adObject.setDiscode(proCode);
			adObject.setProPackWay(pack);
			adObject.setProPrice(doObject.getDisPrice() + "");
//			adObject.setDisTotal(doObject.getDisTotal()+"");
			adObject.setDisTotal((doObject.getDisPrice()*nums)+"");
			adObject.setProNumber(nums + "");
			totalNum = totalNum + nums;
			totalMony = totalMony + (doObject.getDisPrice() * nums);
			
			doObject.setDisNums(nums);

			doObject.setProToNums(numss - nums);
			/**
			 * 对库房预警进行设置
			 */
			Cdstock stockObject = this.getCdstockService().getCdstock(
					doObject.getProductCode(), doObject.getProductName());
			if (stockObject != null) {
				int max_Nums = stockObject.getMax_amount();
				int min_Nums = stockObject.getMin_amount();
				int amount = stockObject.getAmount() - nums;

				if (amount <= max_Nums && amount >= min_Nums) {
					// 没有操作预警的范围
					stockObject.setAmount(amount);
					this.getCdstockService().saveOrStock(stockObject);
				} else {
					// 超出预警，要预警
					StockPrecautionary spobject1 = this
							.getStockPrecautionaryService()
							.getStockPrecautionaryByProductId(
									doObject.getProductCode());
					if (spobject1 != null) {
						spobject1.setProductNum(amount);
						this.getStockPrecautionaryService()
								.saveOrStockPrecautionary(spobject1);
					} else {
						StockPrecautionary spkk = new StockPrecautionary();
						spkk.setProductCode(doObject.getProductCode());
						spkk.setProductDownline(stockObject.getMin_amount());
						spkk.setProductUpline(stockObject.getMax_amount());
						spkk.setProductNum(amount);
						spkk.setProductName(doObject.getProductName());
						SaleOrderDetail dd=this.saleOrderService.getSaleOrderDetailByproductCode(doObject.getSaleOrderPlan());
						CrmCustomer crm=this.crmCustomerService.getCrmCustomerBycusCode(dd.getSaleOrder().getCusCode());
						spkk.setCrmcustomer(crm);
						spkk.setPrTitle("安全库存");
						this.getStockPrecautionaryService().save(spkk);

					}

					stockObject.setAmount(amount);
					this.getCdstockService().saveOrStock(stockObject);
				}

			}
			this.getDistributionOrderService().saveOrDistributionOrderDetail(
					doObject);
			alreadyDistribution.addDetail(adObject);

		}
		alreadyDistribution.setTotalNums(totalNum + "");
		alreadyDistribution.setTotalMony(totalMony);
		
		
		
		
		alreadyDistribution.setDistributionCode(proCode);
		super.save(alreadyDistribution);

		/**
		 * 查询这个配送单是否已经配送完
		 */
		
		int judnUMS = 0;
		for (int i = 0; i < inLengths; i++) {
			DistributionOrderDetail doObject = this
					.getDistributionOrderService().getDistributionOrderDetail(
							proJudges[i]);
			judnUMS = judnUMS + doObject.getProToNums();
		}
		if (judnUMS <= 0) {
			distributionOrder.setState("3");
			this.getDistributionOrderService().saveOrDistributionOrder(
					distributionOrder);
		}

		/**
		 * 进入打回配送的记录
		 */

		DistributionReturn dr = new DistributionReturn();
		dr.setReOrderCode(alreadyDistribution.getPlanorderCode());
		dr.setReCustomerName(alreadyDistribution.getCustomerName());
		dr.setRedisSate(1);
		dr.setReToltalNum(Integer.parseInt(alreadyDistribution.getTotalNums()));
		dr.setReToltalprice(alreadyDistribution.getTotalMony());
		for (int t = 0; t < lengths; t++) {
			DistributionOrderDetail doObject = this
					.getDistributionOrderService().getDistributionOrderDetail(
							checks[t]);
			DistributionReturnDetail adObject = new DistributionReturnDetail();
			adObject.setProCode(doObject.getProductCode());
			adObject.setProName(doObject.getProductName());
			adObject.setProColor(doObject.getProductColor());
			adObject.setProNorm(doObject.getProductNorm());
			adObject.setProPackWay(doObject.getProductPackWay());
			adObject.setProPrice(doObject.getDisPrice());
			adObject.setProSqual(doObject.getDisSquare() + "");
			adObject.setDisNums(doObject.getDisNums());
			adObject.setProToltal(doObject.getDisNums()
					* doObject.getDisPrice());
			dr.addDetail(adObject);
		}
		this.getDistributionReturnService().save(dr);

	}

	public DistributionOrder getDistributionOrder() {
		return distributionOrder;
	}

	public void setDistributionOrder(DistributionOrder distributionOrder) {
		this.distributionOrder = distributionOrder;
	}

	public DistributionOrderService getDistributionOrderService() {
		return distributionOrderService;
	}

	public void setDistributionOrderService(
			DistributionOrderService distributionOrderService) {
		this.distributionOrderService = distributionOrderService;
	}

	public AlreadyDistribution getAlreadyDistribution() {
		return alreadyDistribution;
	}

	public void setAlreadyDistribution(AlreadyDistribution alreadyDistribution) {
		this.alreadyDistribution = alreadyDistribution;
	}

	public AlreadyDistributionService getAlreadyDistributionService() {
		return alreadyDistributionService;
	}

	public void setAlreadyDistributionService(
			AlreadyDistributionService alreadyDistributionService) {
		this.alreadyDistributionService = alreadyDistributionService;
	}

	public Cdstock getCdstock() {
		return cdstock;
	}

	public void setCdstock(Cdstock cdstock) {
		this.cdstock = cdstock;
	}

	public CdstockService getCdstockService() {
		return cdstockService;
	}

	public void setCdstockService(CdstockService cdstockService) {
		this.cdstockService = cdstockService;
	}

	public StockPrecautionaryService getStockPrecautionaryService() {
		return stockPrecautionaryService;
	}

	public void setStockPrecautionaryService(
			StockPrecautionaryService stockPrecautionaryService) {
		this.stockPrecautionaryService = stockPrecautionaryService;
	}

	public SerialNumberssService getSerialNumberssService() {
		return serialNumberssService;
	}

	public void setSerialNumberssService(
			SerialNumberssService serialNumberssService) {
		this.serialNumberssService = serialNumberssService;
	}

	public DistributionReturnService getDistributionReturnService() {
		return distributionReturnService;
	}

	public void setDistributionReturnService(
			DistributionReturnService distributionReturnService) {
		this.distributionReturnService = distributionReturnService;
	}

	public cd_ProductService getCdProductService() {
		return cdProductService;
	}

	public void setCdProductService(cd_ProductService cdProductService) {
		this.cdProductService = cdProductService;
	}

	public CrmCustomerService getCrmCustomerService() {
		return crmCustomerService;
	}

	public void setCrmCustomerService(CrmCustomerService crmCustomerService) {
		this.crmCustomerService = crmCustomerService;
	}

	public SaleOrderService getSaleOrderService() {
		return saleOrderService;
	}

	public void setSaleOrderService(SaleOrderService saleOrderService) {
		this.saleOrderService = saleOrderService;
	}

}
